/**
 * 微信付款码
 */
function wx_code(tbl){
	var order=dao.fetch("orders",Cnd.where("id","=",tbl.id));
	var data = new NutMap();
	data.put("wxpay_appid", "wx2787c3302c182d65");
	data.put("wxpay_mch_id", "1289729501");
	data.put("wxpay_key", "6e332d87778242619fd832c1bc85d12f");
	 data.put("body",order.title);
	 data.put("spbill_create_ip",ip());
	 data.put("total_fee",order.price);
	 data.put("device_info", "WEB");
	 data.put("nonce_str",java.util.UUID.randomUUID().toString().replace("-", ""));
	 data.put("notify_url", "http://www.iepsy.com/pay/wx_notify");
	data.put("detail", order.title);
	data.put("attach", order.title);
	data.put("out_trade_no", order.orderno);
	data.put("fee_type", "CNY");
	data.put("total_fee", order.price);
	data.put("product_id", order.id);
	print(data)
	var url=com.jse.pay.WxPay.getQrcode(data);
	var res=new Record();
	res.set("url",url);
	res.set("status",1);
	return res;
}
//微信返回
function wx_notify(tbl){
	print(tbl)
	try {
		var wxpay=com.wx.Notify.notify(req);
		print(wxpay)
		wxpay.addv(".table","wxpay");
		
		if(wxpay.result_code=="SUCCESS"&&wxpay.return_code=="SUCCESS"){
			var orderno=wxpay.out_trade_no;
			var order = dao.fetch("orders", Cnd.where("orderno", "=", orderno));
			if(order.status == 1){
				return tbl;
			}
			if (order.price*100 != parseInt(wxpay.total_fee)) {
				print("order 价格"+order.price+"|||微信价格"+wxpay.total_fee)
			}
			// 修改订单状态为已支付，并核对支付金额是否于订单金额相符
			// reserve.set("pay_money",tbl.get("total_fee"));//设置支付金额
			order.set("status", 1);
			order.set(".table", "orders");
			dao.update("orders",Chain.make("status",1).add("pay_type",2),Cnd.where("id","=",order.id));//更新订单状态
			//处理订单
			//exe_order(order);
		}else{
			
		}
		$save(wxpay);
	} catch (e) {
		// TODO: handle exception
		print(e)
	}
}

function exe_order(order){
	var carts = foJson(order.carts)
	for (var i = 0; i < carts.size(); i++) {
		var cart = carts.get(i)
		if(cart.table_name=="course"){
		var course = dao.fetch("course", Cnd.where("id", "=", cart.table_id))
		var course_user = new Record();
		//course_user.set("id", uuid());
		course_user.set("course_id", cart.table_id);
		course_user.set("user_id", order.user_id);
		course_user.set("title", cart.title);
		course_user.set("content", cart.text);
		course_user.set("teacher", course.teacher);
		course_user.set("img", cart.img);
		course_user.set(".table", "course_user");
		var dat = new java.util.Date();
		var cal = Calendar.getInstance();
		cal.setTime(dat);
		cal.add(Calendar.MONTH,1);//用Calendar对月加1,绕开判断闰年
		var end_time=cal.getTime();
		course_user.set("end_time", end_time);
		dao.insert(course_user);
		}else if(cart.table_name=="combo"){
			var combo = dao.fetch("combo", Cnd.where("id", "=", cart.table_id))
			var combo_user = new Record();
			combo_user.set("id", uuid());
			combo_user.set("combo_id", cart.table_id);
			combo_user.set("user_id", order.user_id);
			combo_user.set("title", cart.title);
			combo_user.set("text", cart.text);
			combo_user.set("img", cart.img);
			combo_user.set(".table", "combo_user");
			var dat = new java.util.Date();
			var cal = Calendar.getInstance();
			cal.setTime(dat);
			cal.add(Calendar.MONTH,1);//用Calendar对月加1,绕开判断闰年
			var end_time=cal.getTime();
			combo_user.set("end_time", end_time);
			dao.insert(combo_user);
			var courses=foJson(combo.courses);
			courses.forEach(function(course){
				var course_user = new Record();
				//course_user.set("id", uuid());
				course_user.set("course_id", course.id);
				course_user.set("user_id", order.user_id);
				course_user.set("title", cart.title);
				course_user.set("content", cart.text);
				course_user.set("teacher", course.teacher);
				course_user.set("img", cart.img);
				course_user.set(".table", "course_user");
				var dat1 = new java.util.Date();
				var cal1 = Calendar.getInstance();
				cal1.setTime(dat1);
				cal1.add(Calendar.MONTH,1);//用Calendar对月加1,绕开判断闰年
				var end_time1=cal1.getTime();
				course_user.set("end_time", end_time1);
				dao.insert(course_user);
			})
		}else if(cart.table_name=="ke"){
			var ke = dao.fetch("ke", Cnd.where("id", "=", cart.table_id))
			
		}else if(cart.table_name=="vip"){
			var vip = dao.fetch("vip", Cnd.where("id", "=", cart.table_id))
			var u=dao.fetch("users",Cnd.where("username", "=", vip.username))
			var dat = new java.util.Date();
			if(!isEmpty(u.vip_end_time)){
				dat=u.vip_end_time;
			}
			var cal = Calendar.getInstance();
				cal.setTime(dat);
				cal.add(Calendar.YEAR,1);//用Calendar对年加1,绕开判断闰年
			var vip_end_time=cal.getTime();
			if(vip.type=="vip"){
				dao.update("users",Chain.make("vip",1).add("vip_end_time",vip_end_time),Cnd.where("id","=",u.id))
			}else{
				dao.update("users",Chain.make("vip",2).add("vip_end_time",vip_end_time),Cnd.where("id","=",u.id))
			}
			return 1;
		}
	}
}